type Char = "-" | "_" | "*" | "#" | "&";
const getUUID = (char?: Char) => {
  let d = new Date().getTime();
  const uuid = "xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx".replace(
    /[xy]/g,
    function (c) {
      const r = (d + Math.random() * 16) % 16 | 0;
      d = Math.floor(d / 16);
      return (c === "x" ? r : (r & 0x3) | 0x8).toString(16);
    }
  );
  if (char) return uuid.replace(/-/g, char);
  return uuid.replace(/-/g, "");
};
import dayjs from "dayjs";

export const getToDayEndTime = () => {
  return dayjs().add(1, "day").startOf("day");
};

/**
 * 历史时间格式化，例如，几秒前，几天前，几分钟前
 * @param {*} value 时间字符串： 例如：2021-11-29 16:05:10
 * @returns
 */
const getHistoryTime = (value: any) => {
  // 判断是否为空
  if (!value) {
    // customLog(`参数【${value}】不能为空，或者undefined或者null`);
    return;
  }
  // 获取当前时间
  const dateEnd = new Date();
  // 时间格式转化为/，使用new Date()
  const dateBegin = new Date(value);
  // 时间差的毫秒数
  const dateDiff = dateEnd.getTime() - dateBegin.getTime();
  // 计算出相差天数
  const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));
  //超过七天，返回时间
  if (dayDiff > 7) {
    return `${dateBegin.getFullYear()}/${
      dateBegin.getMonth() + 1
    }/${dateBegin.getDay()}`;
  }
  //大于1天，显示周几
  if (dayDiff > 1) {
    const weekDay = [
      "星期天",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",
    ];
    return weekDay[dateBegin.getDay()];
  }
  if (dayDiff >= 0 && dateBegin.getHours() > dateEnd.getHours()) {
    return "昨天";
  }
  return `${dateBegin.getHours()}:${
    dateBegin.getMinutes() > 9
      ? dateBegin.getMinutes()
      : "0" + dateBegin.getMinutes()
  }`;
};

const getEventTime = (value: string) => {
  // 判断是否为空
  if (!value) {
    // customLog(`参数【${value}】不能为空，或者undefined或者null`);
    return;
  }
  // 获取当前时间
  const dateEnd = new Date();
  // 时间格式转化为/，使用new Date()
  const dateBegin = new Date(parseInt(value));
  // 时间差的毫秒数
  const dateDiff = dateEnd.getTime() - dateBegin.getTime();
  // 计算出相差天数
  const dayDiff = Math.floor(dateDiff / (24 * 3600 * 1000));

  const timeString = ` ${dateBegin.getHours()}:${
    dateBegin.getMinutes() > 9
      ? dateBegin.getMinutes()
      : "0" + dateBegin.getMinutes()
  }`;
  //超过七天，返回时间
  if (dayDiff > 7) {
    return dateBegin.toLocaleDateString() + timeString;
  }
  //大于1天，显示周几
  if (dayDiff > 1) {
    const weekDay = [
      "星期天",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六",
    ];
    return weekDay[dateBegin.getDay()] + timeString;
  }
  if (dayDiff >= 0 && dateBegin.getHours() > dateEnd.getHours()) {
    return "昨天 " + timeString;
  }

  return timeString.trim();
};

//是否展示时间
function showEventTime(start: number, end: number) {
  //判断上次的发送时间是否超过五分钟
  const dayDiff = Math.floor((end - start) / (60 * 1000));
  return dayDiff > 5;
}

export { getHistoryTime, getEventTime, showEventTime };

export { getUUID };
